package cn.wpy.controller;

import cn.wpy.pojo.Users;
import cn.wpy.service.UsersService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户信息表(Users)表控制层
 *
 * @since 2024-07-02 15:13:14
 */
@RestController
@CrossOrigin
@Api(tags = "管理员接口", value = "管理员接口")
@RequestMapping("/users")
public class UsersController {

    @Resource
    private UsersService usersService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @ApiOperation("根据id查询")
    @GetMapping("/queryById")
    public ResponseEntity<Users> queryById(@RequestParam("id") Integer id) {
        return ResponseEntity.ok(this.usersService.queryById(id));
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @ApiOperation("查询列表")
    @GetMapping("/queryList")
    public ResponseEntity<List<Users>> queryList() {
        return ResponseEntity.ok(this.usersService.queryList());
    }

    /**
     * 新增数据
     *
     * @param users 实体
     * @return 新增结果
     */
    @ResponseBody
    @ApiOperation("添加数据")
    @PostMapping("/add")
    public ResponseEntity add(Users users) {
        return ResponseEntity.status(200).body(usersService.insert(users));
    }

    /**
     * 修改数据
     *
     * @param users 实体
     * @return 编辑结果
     */
    @ResponseBody
    @ApiOperation("修改数据")
    @PutMapping("/update")
    public ResponseEntity edit(@RequestBody Users users) {
        return ResponseEntity.ok(usersService.update(users));
    }

    /**
     * 删除数据
     *
     * @param userId 主键
     * @return 删除是否成功
     */
    @ResponseBody
    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{userId}")
    public ResponseEntity<Integer> deleteById(@PathVariable Integer userId) {
        return ResponseEntity.ok(usersService.deleteById(userId));
    }

    /**
     * 登陆
     *
     * @param username 用户名
     * @param password 密码
     * @return 登陆结果
     */
    @ResponseBody
    @ApiOperation(value = "登录", httpMethod = "POST")
    @PostMapping("/login")
    public ResponseEntity login(@RequestParam("username") String username, @RequestParam("password") String password) {
        return ResponseEntity.ok(usersService.login(username, password));
    }

    /**
     * 分页查询
     *
     * @param pageNo   当前页码
     * @param pageSize 每页记录数
     * @return 分页数据
     */
    @ResponseBody
    @GetMapping("/pageInfo")
    @ApiOperation(value = "分页查询接口", notes = "分页数据", httpMethod = "GET")
    public ResponseEntity<PageInfo<Users>> pageInfo(
            @RequestParam(required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        PageInfo<Users> managerPage = usersService.getPageInfo(pageNo, pageSize);
        return ResponseEntity.ok(managerPage);
    }
}
